Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/avendre/[uid]/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-11-2612 * Last Modified : 2026-11-2613 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// GET /api/avendre/{uid} — fiche complète d'une propriété à vendre : annonce,43// photos, caractéristiques, historique du prix demandé, estimation Vrai-Prix44// (table vp_eval), unité du rôle jumelée et annonces voisines du même groupe.45// Mêmes données que la page /a-vendre/{uid}.46import { NextRequest } from "next/server";47import { getUnit } from "@/lib/db";48import { GENRE_PAR_SLUG, genreDe } from "@/lib/genres";49import { lecturePrix } from "@/lib/verdict";50import { getListing, listingsNear } from "@/lib/immoka";51import { summaryForListing } from "@/lib/cost/ai/service";52import { SITE_URL } from "@/lib/seo";53import { cache, erreur, json } from "@/lib/api";5455export const dynamic = "force-dynamic";5657export async function GET(_req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {58 const { uid: brut } = await params;59 const uid = decodeURIComponent(brut);60 try {61 const det = getListing(uid);62 if (!det) return erreur(404, "annonce introuvable", "not_found");63 const l = det.listing;64 const u = det.eval?.unit_id ? getUnit(det.eval.unit_id) : undefined;65 const voisines =66 l.lat != null && l.lng != null ? listingsNear(l.lat, l.lng, 3, { group: det.group, excludeUid: uid, limit: 6 }) : [];67 let analyseIA: unknown = null;68 try {69 analyseIA = summaryForListing(uid);70 } catch {71 analyseIA = null;72 }73 const e = det.eval;74 return json(75 {76 listing: {77 uid: l.uid,78 source: l.source,79 url: l.url,80 title: l.title,81 address: l.address,82 sector: l.sector,83 city: l.city,84 region: l.region,85 propertyType: l.property_type,86 group: det.group,87 status: l.status,88 price: l.price,89 bedrooms: l.bedrooms,90 bathrooms: l.bathrooms,91 powderRooms: l.powder_rooms,92 areaSqft: l.area_sqft,93 lotSqft: l.lot_sqft,94 yearBuilt: l.year_built,95 mls: l.mls,96 brokerName: l.broker_name,97 agency: l.agency,98 description: l.description,99 lat: l.lat,100 lng: l.lng,101 firstSeen: l.first_seen,102 lastSeen: l.last_seen,103 },104 images: det.images,105 features: det.features,106 details: det.details,107 priceLog: det.priceLog,108 eval: e109 ? {110 unitId: e.unit_id,111 matchM: e.match_m,112 est: e.est,113 low: e.low,114 high: e.high,115 confidence: e.confidence,116 confidencePct: e.confidence_pct,117 modelEst: e.model_est,118 compsEst: e.comps_est,119 costEst: e.cost_est,120 roleEst: e.role_est,121 ensEst: e.ens_est,122 nComps: e.n_comps,123 valeurRole: e.valeur_role,124 typeProp: e.type_prop,125 municipalite: e.municipalite,126 ratio: e.ratio,127 diff: e.diff,128 evaluatedAt: e.evaluated_at,129 // unité retenue par la seule proximité (lot nocturne), pour mémoire130 unitIdProximite: e.unit_id_proximite ?? null,131 }132 : null,133 // jumelage vérifié de l'inscription au rôle : `eval.est` et le verdict n'existent que s'il est sûr134 jumelage: e?.jumelage ?? { etat: "aucun", raison: "sans-unite", detail: "Aucune unité du rôle à proximité de l'inscription.", unitId: null },135 // lecture du prix demandé face à la valeur de la fiche de l'unité (règle unique : /api/version, thresholds)136 ...lecturePrix(e?.est != null ? l.price : null, e?.est, e?.low, e?.high),137 unit: u138 ? {139 id: u.id_provinc,140 adresse: u.adresse,141 apt: u.apt,142 municipalite: u.municipalite,143 typeProp: u.type_prop,144 genre: genreDe(u.cubf, u.nb_logements, u.apt),145 genreLibelle: GENRE_PAR_SLUG[genreDe(u.cubf, u.nb_logements, u.apt)].singulier,146 // valeur de la fiche de l'unité (la même que eval.est) ; est2026, p10, p90 : le modèle seul147 valeurEstimee: e?.est ?? null,148 anneeConstruction: u.annee_construction,149 aireEtagesM2: u.aire_etages_m2,150 superficieTerrainM2: u.superficie_terrain_m2,151 nbEtages: u.nb_etages,152 nbLogements: u.nb_logements,153 valeurTerrain: u.valeur_terrain,154 valeurBatiment: u.valeur_batiment,155 valeurRole: u.valeur_role,156 est2026: u.est_2026,157 p10: u.p10,158 p90: u.p90,159 history: ([2021, 2022, 2023, 2024, 2025, 2026] as const).map((y) => ({ year: y, value: u[`est_${y}`] })),160 }161 : null,162 voisines,163 analyseIA,164 links: {165 page: `${SITE_URL}/a-vendre/${encodeURIComponent(l.uid)}`,166 comps: `/api/avendre/comps?uid=${encodeURIComponent(l.uid)}`,167 unit: u ? `/api/units/${encodeURIComponent(u.id_provinc)}` : null,168 },169 },170 cache(120)171 );172 } catch (err) {173 return erreur(500, (err as Error).message);174 }175}176